package tester;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.http.HTTPBinding;

import yahoo.prods.ProductSearch;

public class YahooTester {

  private static final String yahooShoppingNS = "http://api.shopping.yahoo.com/ShoppingService";

  private static final String productSearchURL = yahooShoppingNS
  + "/V2/productSearch";

  private static final String USER_AGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";

  private static Dispatch<Source> productSearchDispatch = null;


  private static JAXBContext yahooShoppingContext = null;

  private static String yahooShoppingAppId = "soajavabook";

  public static void main(String[] args) {

    String contextPath = "yahoo.catalogspecs:yahoo.merchinfo:"
      + "yahoo.merchoffers:yahoo.prodrev:yahoo.prods";
    try {
      yahooShoppingContext = JAXBContext.newInstance(contextPath);
    } catch (JAXBException e) {
      throw new RuntimeException("Failed to create JAXBContext from: "
          + contextPath);
    }
    QName shoppingServiceQN = new QName(yahooShoppingNS, "ShoppingService");
    QName productSearchPortQN = new QName(yahooShoppingNS, "ProductSearch");
    Service shoppingService = Service.create(shoppingServiceQN);
    shoppingService.addPort(productSearchPortQN, HTTPBinding.HTTP_BINDING,
        productSearchURL);
    productSearchDispatch = shoppingService.createDispatch(productSearchPortQN,
        Source.class, Service.Mode.PAYLOAD);
    productSearchDispatch.getRequestContext().put(
        MessageContext.HTTP_REQUEST_METHOD, "GET");
  String query = "appid=soajavabook&query=razr&category=Electronics%20%26%20Camera&highestprice=300.00&lowestprice=200.00&results=50";
//  String query = "appid=soajavabook&query=razr&category=Electronics%20%26%20Camera";
//  String query = "appid=soajavabook&query=razr&highestprice=300.00&lowestprice=200.00&results=50";
//  String query = "appid=soajavabook&query=razr";
    productSearchDispatch.getRequestContext().put(MessageContext.QUERY_STRING,
        query);
    setRequestHeaders(productSearchDispatch.getRequestContext());
    try {
      String urlStr = (String)
        productSearchDispatch.getRequestContext().get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
      System.out.println("Dispatching to URL: " + urlStr);
      productSearchDispatch.invoke(null);
    } catch (Exception e) {
      throw new RuntimeException("YahooShopping Product Search: " + 
          productSearchURL + "?" + query + " threw an Exception", e);
    }
    System.out.println("success.");
    
  }

  private static void setRequestHeaders(Map<String, Object> requestContext) {
    
    Map requestHeaders = (Map) requestContext
        .get(MessageContext.HTTP_REQUEST_HEADERS);
    if (requestHeaders == null) {
      requestHeaders = new HashMap<String, String>();
      requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);
    }
    List<String> userAgentList = new ArrayList<String>();
    userAgentList.add(USER_AGENT);
    requestHeaders.put("User-agent", userAgentList);
    
  }
  
}
